package FlappyBirds;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;

/**
 * 目录面版（除music）
 * 
 * @author yb
 *
 */

public class MenuJframe extends JFrame {
	private Container ct;// 创建一个容器
	private BackgroundPanel bgp;// 创建背景面板。
	private JButton start = new JButton("开始游戏");
	private JButton introduction = new JButton("游戏介绍");

	/**
	 * 背景音乐播放
	 * 
	 * @author ll
	 */
	static void playMusic() {
		try {
			AudioInputStream ais = AudioSystem.getAudioInputStream(new File("D:\\Game\\FlappyBirdGame\\music.WAV"));
			AudioFormat aif = ais.getFormat();
			final SourceDataLine sdl;
			DataLine.Info info = new DataLine.Info(SourceDataLine.class, aif);
			sdl = (SourceDataLine) AudioSystem.getLine(info);
			sdl.open(aif);
			sdl.start();
			FloatControl fc = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
			// value可以用来设置音量，从0-2.0
			double value = 2;
			float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
			fc.setValue(dB);
			int nByte = 0;
			final int SIZE = 1024 * 64;
			byte[] buffer = new byte[SIZE];
			while (nByte != -1) {
				nByte = ais.read(buffer, 0, SIZE);
				sdl.write(buffer, 0, nByte);
			}
			sdl.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Thread(() -> {
			while (true) {
				MenuJframe.playMusic();
			}
		}).start();
		new MenuJframe();
	}

	public MenuJframe() {
		ct = this.getContentPane();
		this.setLayout(null);
		// 先加其他的再放背景图
		start.setBounds(200, 300, 160, 30);// 设置按钮位置
		introduction.setBounds(200, 400, 160, 30);
		start.setBackground(Color.pink);// 设置按钮颜色
		introduction.setBackground(Color.pink);// 设置按钮颜色
		ct.add(start);// 加入按钮
		ct.add(introduction);

		bgp = new BackgroundPanel(new ImageIcon("D:\\Game\\FlappyBirdGame\\9.png").getImage());
		bgp.setBounds(0, 0, 600, 600);
		ct.add(bgp);// 加入背景图

		JPanel panel = new GifPanel();
		panel.setBounds(0, 0, 100, 100);
		panel.setOpaque(true);// 使窗口透明
		ct.add(panel);

		this.setTitle("flappy character");// 窗口标题
		this.setSize(600, 600);// 窗口大小
		this.setResizable(false);// 禁止调制窗口大小
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);// 让窗体显示在屏幕正中间
		this.setVisible(true);

		start.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ee) {
				dispose();
				ImageSelection is = new ImageSelection();
				// GameClient game=new GameClient();

			}
		});
		introduction.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ee) {
				JOptionPane.showMessageDialog(null, "单击鼠标，使得所选人物向上移动，人物通过管道则加1分。\n 注意：当人物触碰到管道时游戏结束！");
			}
		});

	}
}

/**
 * 显示背景图片
 * 
 * @author yb
 *
 */
class BackgroundPanel extends JPanel {
	Image im;

	public BackgroundPanel(Image im) {
		this.im = im;
		this.setOpaque(true);
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponents(g);
		g.drawImage(im, 0, 0, this.getWidth(), this.getHeight(), this);

	}
}

/**
 * 显示gif图
 * 
 * @author yb
 *
 */

class GifPanel extends JPanel {
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		ImageIcon icon = new ImageIcon("D:\\Game\\FlappyBirdGame\\11.gif");
		g.drawImage(icon.getImage(), 0, 0, 100, 100, this);
	}
}
